﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace CSMF.Management.Page_Video
{
    public partial class Video_Type : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    BindDDLType();
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }
        }

        private void BindDDLType()
        {
            IList<VideoType> types = VideoTypeManager.GetAllVideoTypes();
            ddlDelType.Items.Clear();
            ddlDelType.Items.Add(new ListItem("-请选择需要删除的类别-", ""));
            ddlUpType.Items.Clear();
            ddlUpType.Items.Add(new ListItem("-请选择需要修改的类别-", ""));
            foreach (VideoType type in types)
            {
                ListItem li = new ListItem();
                li.Value = type.ID.ToString();
                li.Text = type.Title;
                ddlDelType.Items.Add(li);
                ddlUpType.Items.Add(li);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtAddCTitle.Text.Trim() != "")
            {
                VideoType ptype = new VideoType();
                ptype.Title = txtAddCTitle.Text.Trim();
                VideoTypeManager.AddVideoType(ptype);
                BindDDLType();

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('新增类别成功!');", true);

            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请输入类别的名称!');", true);
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            if (ddlDelType.SelectedValue != "")
            {
                VideoTypeManager.DeleteVideoTypeByID(Convert.ToInt32(ddlDelType.SelectedValue));
                BindDDLType();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请选择需要删除的类别名称!');", true);
            }
        }

        protected void btnUpdata_Click(object sender, EventArgs e)
        {
            if (ddlUpType.SelectedValue != "")
            {
                VideoType ptype = VideoTypeManager.GetVideoTypeByID(Convert.ToInt32(ddlUpType.SelectedValue));
                ptype.Title = txtUpCTitle.Text.Trim();
                VideoTypeManager.ModifyVideoType(ptype);
                BindDDLType();

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('修改类别成功!');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请选择需要修改的类别名称!');", true);
            }
        }

        protected void ddlUpType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlUpType.SelectedValue != "")
            {
                VideoType ptype = VideoTypeManager.GetVideoTypeByID(Convert.ToInt32(ddlUpType.SelectedValue));
                txtUpCTitle.Text = ptype.Title;
            }
        }
    }
}
